package com.marce.remis.flexClient.components.base
{
	import com.marce.remis.flexClient.components.LoginPanel;
	import com.marce.remis.flexClient.components.MyPanel;
	import com.marce.remis.flexClient.events.LoginEvent;
	import com.marce.remis.flexClient.model.ModelLocator;
	import com.marce.remis.flexClient.model.UsuarioModel;
	import com.marce.remis.flexClient.vo.Usuario;
	
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	import mx.events.FlexEvent;

	public class LoginPanelBase extends MyPanel
	{
		private var _view:LoginPanel;
		
		protected var model:UsuarioModel;
		public function LoginPanelBase()
		{
			super();
			this._view = this as LoginPanel;
			model = ModelLocator.instance.usuarioModel;
			this.addEventListener(FlexEvent.CREATION_COMPLETE,onCreationComplete,false,0,true);
		}
		
		private function onCreationComplete(event:Event):void{
			this._view.userName.setFocus();
		}
		
		protected function onSubmit():void{
			var usuario:Usuario = model.usuario;
			usuario.userName = this._view.userName.text;
			usuario.password = this._view.password.text;
			new LoginEvent(usuario).dispatch();
		}
		
		protected function onCancel():void{
			this._view.userName.text = "";
			this._view.password.text = "";
		}
		
		protected function onEnter(event:KeyboardEvent):void{
			if(event.keyCode==Keyboard.ENTER) onSubmit();
		}
	}
}